<?php

namespace app\model;


use think\facade\Db;
use think\response\Json;
use Exception;

class SchoolRoleModel
{
    private static $_table = 'eb_school_role'; // 学校表
    private static $_schoolStoreTable = 'eb_school_store';
    private static $_orderTable = 'eb_order';
    /**列表
     * @param $input
     * @param $token
     * @return Json
     */
    public static function list($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['operate_id', '=', $adminInfo['id']],
                ['school_id', '=', $input['school_id']]
            ];
            if ($input['type']) $map[] = ['type', '=', $input['type']];
            if ($input['name']) $map[] = ['name', 'like', "%{$input['name']}%"];
            $field = 'id,name,phone, FROM_UNIXTIME(create_time, "%Y-%m-%d %H:%i:%s") AS create_time, type,status';
            $table = Db::table(self::$_table)
                ->field($field)->where($map);
            $count = $table->count();
            if ($count == 0) return jsonSuccess('获取成功', ['count' => 0, 'list' => []]);
            $list = $table->page($input['page'], $input['limit'])->order('create_time', 'desc')->select()->toArray();
            return jsonSuccess('获取成功！', ['count' => $count, 'list' => $list]);
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**添加
     * @param $input
     * @param $token
     * @return Json
     */
    public static function add($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $whereSql = Db::table(self::$_table)->field('id')->where('phone', $input['phone'])->fetchSql(true)->find();
            $where = "not exists ({$whereSql})";
            $input['operate_id'] = $adminInfo['id'];
            $input['status'] = 1;
            $input['create_time'] = time();
            $sql = makeDualSql(self::$_table, $input, $where);
            $rtn = Db::table(self::$_table)->execute($sql);
            if ($rtn) return jsonSuccess('添加成功');
            return jsonFail('电话已被使用，请使用其他手机号');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**修改管理员状态
     * @param $input
     * @param $token
     * @return Json
     */
    public static function changeStatus($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['school_role_id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']],
                ['status', '=', $input['status'] == 1 ? 0 :1]
            ];
            $rtn = Db::table(self::$_table)->where($map)->update(['status' => $input['status']]);
            if ($rtn) return jsonSuccess('修改成功');
            return jsonFail('修改失败，请刷新页面重试');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取单个管理员
     * @param $input
     * @param $token
     * @return Json
     */
    public static function getOnly($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            $rtn = Db::table(self::$_table)->field('id,name,phone,type,school_id')->where($map)->find();
            if ($rtn) return jsonSuccess('获取成功',$rtn);
            return jsonFail('未找到数据');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**编辑管理员
     * @param $input
     * @param $token
     * @return Json
     */
    public static function edit($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            unset($input['id']);
            unset($input['school_id']);
            $rtn = Db::table(self::$_table)->where($map)->update($input);
            if ($rtn) return jsonSuccess('修改成功');
            return jsonFail('修改失败，请刷新页面重试');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
    /**删除管理员
     * @param $input
     * @param $token
     * @return Json
     */
    public static function delete($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            $rtn = Db::table(self::$_table)->where($map)->delete();
            if ($rtn) return jsonSuccess('删除成功');
            return jsonFail('删除失败，请刷新页面重试');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取对应类型管理员
     * @param $input
     * @param $token
     * @return Json
     */
    public static function getRoleTypeList($input, $token)
    {
        // 检测当前管理员状态
        $adminInfo = AdminModel::getInfo($token, [1], [2]);
        if ($adminInfo instanceof Json) return $adminInfo;
        $schoolId = $input['school_id'];
        $operateId = $adminInfo['id'];
        $type = $input['type'];
        $rtn = self::getRoleTypeListStatic($schoolId, $operateId,$type);
        if (is_array($rtn)) return jsonSuccess('获取成功', $rtn);
        return $rtn;
    }

    /**获取对应类型管理员
     * @param $schoolId
     * @param $operateId
     * @param $type
     * @return array|Json
     */
    public static function getRoleTypeListStatic($schoolId, $operateId,$type)
    {
        try {

            $map = [
                ['school_id', '=', $schoolId],
                ['operate_id', '=', $operateId],
                ['type', '=', $type],
                ['status', '=', 1]
            ];
            $rtn = Db::table(self::$_table)->field('id,name')->where($map)->select()->toArray();
            if ($rtn) return $rtn;
            return jsonFail('缺少可用的管理员');
        } catch (Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
    /**获取商家列表
     * @param $input
     * @return Json
     */
    public static function Store_list($input)
    {
        try {
            $eb_school_store = Db::table(self::$_schoolStoreTable)->where('school_id', $input['school_id'])->select()->toArray();
            $storeIDs = array_column($eb_school_store, 'store_id');//商家ID集合
            $data  = Db::table('eb_store')->order('sort', 'DESC')->where('id', 'in', $storeIDs)->field('id,shop_name')->select()->toArray();
            return jsonSuccess('获取成功',$data);
        } catch (Exception $e) {
            return jsonFail($e->getMessage());
        }
    }

    /**获取订单列表
     * @param $input
     * @return Json
     */
    public static function Order_list($input)
    {
        try {
            if($input['status']!=2){
                $data = Db::table(self::$_orderTable)->where('store_id',$input['store_id'])->where('financial_status',$input['status'])->where('is_pay',1)->whereTime('create_time', 'between', [$input['start_time'], $input['end_time']])->field('id,order_no,financial_status,goods_price')->select()->toArray();
            }else{
                $data = Db::table(self::$_orderTable)->where('store_id',$input['store_id'])->where('is_pay',1)->whereTime('create_time', 'between', [$input['start_time'], $input['end_time']])->field('id,order_no,financial_status,goods_price')->select()->toArray();
            }
            return jsonSuccess('获取成功',$data);
        } catch (Exception $e) {
            return jsonFail($e->getMessage());
        }
    }

    /**获取订单列表
     * @param $input
     * @return Json
     */
    public static function order_modify($input)
    {
        try {
            $ids = explode(',',$input['ids']);
            if(empty($ids)) return jsonFail('参数格式错误');
            $res = Db::table(self::$_orderTable)->where('id', 'in', $ids)->update(['financial_status'=>1]);
            if($res!=0) return jsonSuccess('修改成功',$res);
            return jsonFail('修改失败');
        } catch (Exception $e) {
            return jsonFail($e->getMessage());
        }
    }
}